package com.example;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity

{
    private String PASSWORD;
    private EditText password;
    private Button loginButton;
    private Button quitButton;
    private Button configPasswordButton;

    SharedPreferences sharedPref;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        Context context = LoginActivity.this;
        sharedPref = context.getSharedPreferences(
                "password_cfg", Context.MODE_PRIVATE);

        PASSWORD = sharedPref.getString("password", null);
        password = (EditText) this.findViewById(R.id.password);
        loginButton = (Button) this.findViewById(R.id.loginButton);
        quitButton = (Button) this.findViewById(R.id.quitButton);
        configPasswordButton = (Button) this.findViewById(R.id.configPasswordButton);


        loginButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String pass = password.getText().toString();
                Log.i(LoginActivity.class.getName(), pass);
                if ("".equals(pass.trim())) {
                    Toast.makeText(LoginActivity.this, "please input the password!", Toast.LENGTH_LONG).show();
                } else {
                    PASSWORD = sharedPref.getString("password", null);
                    if (PASSWORD == null) PASSWORD = "123456";
                    if (PASSWORD.equals(pass)) {
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(LoginActivity.this, "password is wrong!", Toast.LENGTH_LONG).show();
                    }
                }
            }
        });
        quitButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

            }
        });
        configPasswordButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, PasswordConfigActivity.class);
                startActivity(intent);
            }
        });
    }
}
